package com.zsq.student.dao;

import com.db.DatabaseConnector;
import com.db.IKcbDao; // 假设您有一个接口 IKcbDao
import com.zsq.student.dto.KcbDTO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 课程DAO类
 */
public class KcbDAO implements IKcbDao {

    public static void main(String[] args) {
        KcbDTO kcbDTO = new KcbDTO("002", "数学", "秦老师", "8班");

        // 定义接口
        IKcbDao kcbDao = new KcbDAO();

        // 创建课程
        boolean created = kcbDao.create(kcbDTO);
        System.out.println("课程创建成功: " + created);

        // 查询全部课程
        List<KcbDTO> courses = kcbDao.selectAll();
        System.out.println("当前所有课程: " + courses);

        // 根据课程ID查询课程
        KcbDTO queriedCourse = kcbDao.getCourseById("001");
        System.out.println("查询到的课程: " + queriedCourse);

        // 更新课程信息
        // 修改课程名称
        kcbDTO.setCourseName("高级数学");
        boolean updated = kcbDao.update(kcbDTO);
        System.out.println("课程更新成功: " + updated);

        // 再次查询全部课程
        courses = kcbDao.selectAll();
        System.out.println("更新后的所有课程: " + courses);

        // 删除课程
        boolean deleted = kcbDao.delete("001");
        System.out.println("课程删除成功: " + deleted);

        // 最后查询全部课程
        courses = kcbDao.selectAll();
        System.out.println("删除后的所有课程: " + courses);
    }


    /**
     * 创建课程
     */
    @Override
    public boolean create(KcbDTO kcbDTO) {
        boolean flag = false;
        PreparedStatement ps = null;
        Connection connection = null;

        try {
            // 数据库连接参数
            DatabaseConnector databaseConnector = new DatabaseConnector();
            connection = databaseConnector.getDBConnection();

            String sql = "INSERT INTO `kcb` (`t_id`, `course_name`, `t_name`, `t_class`) VALUES (?, ?, ?, ?);";
            ps = connection.prepareStatement(sql);

            ps.setString(1, kcbDTO.gettId());
            ps.setString(2, kcbDTO.getCourseName());
            ps.setString(3, kcbDTO.gettName());
            ps.setString(4, kcbDTO.gettClass());

            int count = ps.executeUpdate();
            // 如果受影响的行数大于0，则创建成功
            flag = count > 0;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            DatabaseConnector.closeDB(null, ps, connection);
        }
        return flag;
    }

    /**
     * 查询全部课程
     */
    @Override
    public List<KcbDTO> selectAll() {
        List<KcbDTO> courses = new ArrayList<>();
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            // 数据库连接参数
            DatabaseConnector databaseConnector = new DatabaseConnector();
            connection = databaseConnector.getDBConnection();

            String sql = "SELECT `t_id`, `course_name`, `t_name`, `t_class` FROM `kcb`;";
            ps = connection.prepareStatement(sql);
            rs = ps.executeQuery();

            while (rs.next()) {
                KcbDTO kcbDTO = new KcbDTO(
                        rs.getString("t_id"),
                        rs.getString("course_name"),
                        rs.getString("t_name"),
                        rs.getString("t_class")
                );
                courses.add(kcbDTO);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            DatabaseConnector.closeDB(rs, ps, connection);
        }
        return courses;
    }

    /**
     * 根据课程ID查询课程
     */
    @Override
    public KcbDTO getCourseById(String courseId) {
        KcbDTO kcb = null;
        String query = "SELECT `t_id`, `course_name`, `t_name`, `t_class` FROM `kcb` WHERE `t_id` = ?";

        try (Connection connection = new DatabaseConnector().getDBConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(query)) {

            preparedStatement.setString(1, courseId);
            ResultSet resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) {
                kcb = new KcbDTO(
                        resultSet.getString("t_id"),
                        resultSet.getString("course_name"),
                        resultSet.getString("t_name"),
                        resultSet.getString("t_class")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace(); // 考虑使用日志框架
        }

        return kcb;
    }

    /**
     * 更新课程信息
     */
    @Override
    public boolean update(KcbDTO kcbDTO) {
        boolean flag = false;
        Connection connection = null;
        PreparedStatement ps = null;

        try {
            // 数据库连接参数
            DatabaseConnector databaseConnector = new DatabaseConnector();
            connection = databaseConnector.getDBConnection();

            String sql = "UPDATE `kcb` SET `course_name` = ?, `t_name` = ?, `t_class` = ? WHERE `t_id` = ?;";
            ps = connection.prepareStatement(sql);

            ps.setString(1, kcbDTO.getCourseName());
            ps.setString(2, kcbDTO.gettName());
            ps.setString(3, kcbDTO.gettClass());
            ps.setString(4, kcbDTO.gettId());

            int count = ps.executeUpdate();
            // 如果受影响的行数大于0，则更新成功
            if (count > 0) {
                // 如果受影响的行数大于0，则删除成功
                flag = true;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            DatabaseConnector.closeDB(null, ps, connection);
        }
        return flag;
    }

    /**
     * 删除课程
     */
    @Override
    public boolean delete(String courseId) {
        boolean flag = false;
        Connection connection = null;
        PreparedStatement ps = null;

        try {
            // 数据库连接参数
            DatabaseConnector databaseConnector = new DatabaseConnector();
            connection = databaseConnector.getDBConnection();

            String sql = "DELETE FROM `kcb` WHERE `t_id` = ?;";
            ps = connection.prepareStatement(sql);
            ps.setString(1, courseId);

            int count = ps.executeUpdate();
            if (count > 0) {
                // 如果受影响的行数大于0，则删除成功
                flag = true;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            DatabaseConnector.closeDB(null, ps, connection);
        }
        return flag;
    }
}
